## Paper: "Politically Heterogeneous Acquaintances and Close Relationships 
###        are Widespread, and Relate to Lower Partisan Animosity"
## Replication data: Main Analysis, all figures in paper
## May, 2025

# Theme for plots

theme_mechs <- theme(
  panel.grid = element_blank(),
  plot.title = element_text(size = 20, family = "CM Roman", hjust = 0.5),
  axis.text = element_text(size = 20, family = "CM Roman", color = "black"),
  axis.title.y = element_text(size = 20, color = "black", family = "CM Roman"),
  axis.title.x = element_text(size = 20, color = "black", family = "CM Roman"),
  panel.background = element_rect(fill = "white"),
  text = element_text(size = 20, colour = "black"),
  axis.ticks = element_blank(),
  legend.text = element_text(size = 20, family = "CM Roman"))

colors <- c("darkgrey", "black")





          ######################################################
          ######################################################
          #### 1. Distribution network heterogeneity ###########
          ######################################################
          ######################################################

# Figure 2 paper

data <- data %>%
  mutate(ard2 = case_when(
    ard_LB == "0" ~ 0,
    ard_LB > "0" & ard_LB <= "0.1" ~ 0.1,
    ard_LB > "0.1" & ard_LB <= "0.2" ~ 0.2,
    ard_LB > "0.2" & ard_LB <= "0.3" ~ 0.3,
    ard_LB > "0.3" & ard_LB <= "0.4" ~ 0.4,
    ard_LB > "0.4" & ard_LB <= "0.5" ~ 0.5,
    ard_LB > "0.5" & ard_LB <= "0.6" ~ 0.6,
    ard_LB > "0.6" & ard_LB <= "0.7" ~ 0.7,
    ard_LB > "0.7" & ard_LB <= "0.8" ~ 0.8,
    ard_LB > "0.8" & ard_LB <= "0.9" ~ 0.9,
    ard_LB > "0.9" & ard_LB <= "1" ~ 1))

x <- data %>% drop_na(close_network, ard2)

data$ard2 <- as.factor(data$ard2)

x_proportions <- data %>% filter(!is.na(ard2))

x_proportions <- drop_na(x_proportions,close_network)


x_proportions <- x_proportions %>%
  group_by(close_network, ard2) %>%
  dplyr::summarise(n = n()) %>%
  group_by(close_network) %>%
  mutate(proportion = n/sum(n))

distribution_networks <- ggplot(x_proportions, aes(x = ard2, y = proportion, color = close_network, fill = close_network)) +
  geom_col(position = "stack") + 
  #theme_mechs +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_manual(values = c("black", "grey")) + 
  scale_color_manual(values = c("black", "grey")) +
  ggtitle("") +
  xlab("Proportion out-partisans in entire acquaintance network") + 
  ylab("Proportion respondents in sample") +
  labs(
    fill = "Close network",
    color = "Close network"
  ) +
  theme(
    plot.title = element_text(size = 20, face = "bold", hjust = 0),
    text = element_text(size = 20, colour = "black"),
    axis.text.y = element_text(size = 12, color = "black"),
    axis.text.x = element_text(size = 12, color = "black"),
    axis.title.y = element_text(size = 20, color = "black"),
    axis.title.x = element_text(size = 20, color = "black"))






## Check whether it's similar in the second dataset
data2 <- data2 %>%
  mutate(ard2 = case_when(
    ard_LB == "0" ~ 0,
    ard_LB > "0" & ard_LB <= "0.1" ~ 0.1,
    ard_LB > "0.1" & ard_LB <= "0.2" ~ 0.2,
    ard_LB > "0.2" & ard_LB <= "0.3" ~ 0.3,
    ard_LB > "0.3" & ard_LB <= "0.4" ~ 0.4,
    ard_LB > "0.4" & ard_LB <= "0.5" ~ 0.5,
    ard_LB > "0.5" & ard_LB <= "0.6" ~ 0.6,
    ard_LB > "0.6" & ard_LB <= "0.7" ~ 0.7,
    ard_LB > "0.7" & ard_LB <= "0.8" ~ 0.8,
    ard_LB > "0.8" & ard_LB <= "0.9" ~ 0.9,
    ard_LB > "0.9" & ard_LB <= "1" ~ 1))










                ######################################################
                ######################################################
                ########### 2. Heterogeneity and animosity ###########               
                ######################################################
                ######################################################







##### 
##### 1. Regressions network homogeneity and animosity
#####

## Figure 3 paper

# Models 

Affect_close <- lm(outparty_final ~ close_network + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, 
                   data = data)

Affect_ARD_LB <- lm(outparty_final ~ Acquaintance_dummy  + Gender + Age + Education + Race
                    + Ideology + Political_interest + Strong_partisan, 
                    data = data)

Affect_close <- tidy(Affect_close) %>% filter(term == "close_networkHeterogeneous")
Affect_ARD_LB <- tidy(Affect_ARD_LB) %>% filter(term == "Acquaintance_dummyHeterogeneous")

Affect_close$value <- "Heterogeneous close network"
Affect_ARD_LB$value <- "High proportion out-partisan acquaintances"

merged <- bind_rows(Affect_close, Affect_ARD_LB)

merged$value <- factor(merged$value, ordered = TRUE, 
                       levels = c("High proportion out-partisan acquaintances",
                                  "Heterogeneous close network"))

merged$term <- NA

networks_animosity <- ggplot(merged, aes(x = value, y = estimate)) + 
  geom_line(aes(group = value), color = "black") + 
  geom_errorbar(aes(ymin = estimate - std.error * 1.96, ymax = estimate + std.error * 1.96), width = 0, color = "black", size = 2) + 
  #theme_mechs +
  geom_point(color = "black", size = 4) + 
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme(text = element_text(size=15)) +
  ggtitle("Network heterogeneity and out-partisan affect") +
  xlab("") +
  ylab("Effect on out-partisan affect (0-1 index)") +
  theme(
    plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
    text = element_text(size = 20, colour = "black"),
    axis.text.y = element_text(size = 20, color = "black"),
    axis.text.x = element_text(size = 20, color = "black"),
    axis.title.y = element_text(size = 20, color = "black", face = "bold"),
    axis.title.x = element_text(size = 20, color = "black"),
    panel.grid.major = element_line(color = "lightblue", size = 0.2, linetype = 1)
  ) 





######
###### Figure 4 paper: competence task by network composition
######

# By close NW homogeneity

Puzzle_closenetwork_homogeneous <- lm(outcome_puzzle ~ treatment_puzzle + Gender + Age + Education + Race
                             + Ideology + Political_interest + Strong_partisan, 
                             data = subset(data, close_network == "Homogeneous"))
Puzzle_closenetwork_heterogeneous <- lm(outcome_puzzle ~ treatment_puzzle + Gender + Age + Education + Race
                               + Ideology + Political_interest + Strong_partisan, 
                               data = subset(data, close_network == "Heterogeneous"))


# By acquaintance network homogeneity

Puzzle_ard_homogeneous <- lm(outcome_puzzle ~ treatment_puzzle + Gender + Age + Education + Race
                             + Ideology + Political_interest + Strong_partisan, 
                             data = subset(data, Acquaintance_dummy == "Homogeneous"))
Puzzle_ard_heterogeneous <- lm(outcome_puzzle ~ treatment_puzzle + Gender + Age + Education + Race
                               + Ideology + Political_interest + Strong_partisan, 
                               data = subset(data, Acquaintance_dummy == "Heterogeneous"))

## Plot

Puzzle_closenetwork_homogeneous <- tidy(Puzzle_closenetwork_homogeneous) %>% filter(term == "treatment_puzzle")
Puzzle_closenetwork_heterogeneous <- tidy(Puzzle_closenetwork_heterogeneous) %>% filter(term == "treatment_puzzle")
Puzzle_ard_homogeneous <- tidy(Puzzle_ard_homogeneous) %>% filter(term == "treatment_puzzle")
Puzzle_ard_heterogeneous <- tidy(Puzzle_ard_heterogeneous) %>% filter(term == "treatment_puzzle")

Puzzle_closenetwork_homogeneous$network <- "Close network"
Puzzle_closenetwork_homogeneous$value <- "Homogeneous/Low proportion out-partisans"

Puzzle_closenetwork_heterogeneous$network <- "Close network"
Puzzle_closenetwork_heterogeneous$value <- "Heterogeneous/High proportion out-partisans"

Puzzle_ard_homogeneous$network <- "Acquaintance network"
Puzzle_ard_homogeneous$value <- "Homogeneous/Low proportion out-partisans"

Puzzle_ard_heterogeneous$network <- "Acquaintance network"
Puzzle_ard_heterogeneous$value <- "Heterogeneous/High proportion out-partisans"


x <- bind_rows(Puzzle_closenetwork_homogeneous, Puzzle_closenetwork_heterogeneous, 
               Puzzle_ard_homogeneous, Puzzle_ard_heterogeneous)

colors <- c("darkgrey", "black")


Puzzle_outcome <- ggplot(x, aes(x = network, y = estimate, fill = value, color = value)) + 
  geom_errorbar(aes(ymin = estimate - std.error * 1.96, ymax = estimate + std.error * 1.96, color = value), width = 0, size = 2) + 
  #theme_mechs +
  geom_point(aes(color = value), size = 4) + 
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme(text = element_text(size=15)) +
  ggtitle("Puzzle") +
  xlab("") +
  ylab("Likelihood of selecting out-partisan engineer in treatment condition") +
  theme(
    plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
    text = element_text(size = 20, colour = "black"),
    axis.text.y = element_text(size = 20, color = "black"),
    axis.text.x = element_text(size = 20, color = "black"),
    axis.title.y = element_text(size = 20, color = "black", face = "bold"),
    axis.title.x = element_text(size = 20, color = "black"),
    panel.grid.major = element_line(color = "lightblue", size = 0.2, linetype = 1),
    legend.title = element_blank(),
    legend.position = "bottom",
    legend.direction = "horizontal",
    legend.text = element_text(size = 12)) +
  scale_fill_manual(values = colors) + 
  scale_color_manual(values = colors)




















###############################################################################
####################### ANALYSIS STUDY 2 (MISPERCEPTIONS AND EXPERIMENT) ######
###############################################################################




#####################################################
###########  HETEROGENEITY AND MISPERCEPTIONS #######
#####################################################


## Figure 5 paper 

MMAll_propLB <- lm(mean_error_all ~ ard_LB + Gender + Age + Education + Race
                   + Ideology + Strong_partisan + Political_interest, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

data2_subset <- subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0)
data2_subset$predicted_mean_error_all <- predict(MMAll_propLB, newdata = data2_subset)

Misp_plot <- ggplot(data2_subset, aes(x = ard_LB, y = predicted_mean_error_all)) + 
  geom_smooth(method = "lm", color = "black") + 
  ggtitle("") +
  labs(y = "Predicted mean error (* 100%)", x = "Proportion out-partisan acquaintances") +
  #theme_mechs + 
  theme(
    plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
    text = element_text(size = 20, colour = "black"),
    axis.text = element_text(size = 20, color = "black"),
    axis.title.y = element_text(size = 20, color = "black", face = "bold"),
    axis.title.x = element_text(size = 20, color = "black", face = "italic"))



                ###################################
                #########   EXPERIMENT  ###########
                ###################################

ATE_one_tie <- lm(outparty_final ~ Treatment + Gender + Age + Education + Race
                  + Ideology + Strong_partisan + Political_interest, 
                  data = subset(data2, ardLB_inpartyties > 0 & ardLB_outpartyties > 0))

## Plot

ATE_one_tie <- tidy(ATE_one_tie) %>% filter(term == "TreatmentTreatment")

estimate <- c(ATE_one_tie$estimate)

se <- c(ATE_one_tie$std.error)

names <- c("Treatment effect")

merged <- data.frame(names, estimate, se)

Treatment_effects <- ggplot(merged, aes(x = names, y = estimate)) + 
  geom_line(aes(group = names), color = "black") + 
  geom_errorbar(aes(ymin = estimate - se * 1.96, ymax = estimate + se * 1.96), width = 0, color = "black", size = 2) + 
  #theme_mechs +
  geom_point(color = "black", size =4) + 
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ggtitle("") +
  xlab("") +
  ylab("Change in out-partisan affect, 0-1 scale") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.x = element_text(size = 12),
        axis.title.x = element_text(size = 12),
        panel.grid.major = element_line(color = "lightblue", size = 0.2, linetype = 1)
  )













          ##############################################################
          #########   TALKING POLITICS AND NW HETEROGENEITY  ###########
          ##############################################################


## Figure 6 paper


# Homogeneous network --> regression talking politics and animosity 

Affect_close_hom_talk <- lm(outparty_final ~ talking_politics_total + total_ties + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, 
                   data = subset(data, close_network == "Homogeneous"))

# Heterogeneous network --> regression talking politics with heterogeneous ties and animosity 
Affect_close_het_talk <- lm(outparty_final ~ talking_outparty_total + talking_inparty_total + total_ties + 
                              Gender + Age + Education + Race
                            + Ideology + Political_interest + Strong_partisan, 
                            data = subset(data, close_network == "Heterogeneous"))

Affect_close_hom_talk <- tidy(Affect_close_hom_talk) %>% filter(term == "talking_politics_total")
Affect_close_het_talk <- tidy(Affect_close_het_talk) %>% filter(term == "talking_outparty_total")

Affect_close_hom_talk$value <- "Talking Politics with In-Partisan Ties"
Affect_close_het_talk$value <- "Talking Politics with Out-Partisan Ties"

merged <- bind_rows(Affect_close_hom_talk, Affect_close_het_talk)

merged$value <- factor(merged$value, ordered = TRUE, 
                       levels = c("Talking Politics with In-Partisan Ties",
                                  "Talking Politics with Out-Partisan Ties"))

merged$term <- NA

talking_animosity <- ggplot(merged, aes(x = value, y = estimate)) + 
  geom_line(aes(group = value), color = "black") + 
  geom_errorbar(aes(ymin = estimate - std.error * 1.96, ymax = estimate + std.error * 1.96), width = 0, color = "black", size = 2) + 
  #theme_mechs +
  geom_point(color = "black", size = 4) + 
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme(text = element_text(size=15)) +
  ggtitle("Talking Politics with Ties") +
  xlab("") +
  ylab("Effect on out-partisan affect (0-1 index)") +
  theme(
    plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
    text = element_text(size = 20, colour = "black"),
    axis.text.y = element_text(size = 20, color = "black"),
    axis.text.x = element_text(size = 20, color = "black"),
    axis.title.y = element_text(size = 20, color = "black", face = "bold"),
    axis.title.x = element_text(size = 20, color = "black"),
    panel.grid.major = element_line(color = "lightblue", size = 0.2, linetype = 1)
  ) 


# Same for experimental outcomes

x <- lm(outcome_puzzle ~ treatment_puzzle * talking_politics_total + 
          gender + age + education + race + ideology + Political_interest + 
          strong_partisan + total_ties, data = subset(data, close_network == "Homogeneous"))

y <- lm(outcome_puzzle ~ treatment_puzzle * talking_outparty_total + talking_inparty_total + 
          gender + age + education + race + ideology + Political_interest + 
          strong_partisan + total_ties, data = subset(data, close_network == "Heterogeneous"))



## Move on to the 'Appendix' datafile




